#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.db import models
from django.core.validators import MaxValueValidator
import os
import sys
import django
from web.util.datetime_util import DatetimeUtil
from web.constants.datetime_format import DatetimeFormat

sys.path.append(r"web")
sys.path.append(r"algorithm")
sys.path.append(r"cta")

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'adam3.settings')
django.setup()


class CommodityFutureAnalysisDetail(models.Model):
    """
    商品期货分析（详细）
    """

    # def __init__(self) -> None:
    #     super().__init__()

    # 主键
    id_ = models.AutoField(primary_key=True)

    # 期货代码
    code = models.CharField(max_length=32, db_index=True, null=True, blank=True)

    # 买入日期
    buy_date = models.DateField(null=True, db_index=True, blank=True)

    # 买入价格
    buy_price = models.DecimalField(max_digits=32, decimal_places=16, null=True, blank=True)

    # 买入手数
    buy_lot = models.IntegerField(validators=[MaxValueValidator(2147483647)], null=True, blank=True)

    # 卖出日期
    sell_date = models.DateField(null=True, db_index=True, blank=True)

    # 卖出价格
    sell_price = models.DecimalField(max_digits=32, decimal_places=16, null=True, blank=True)

    # 卖出手数
    sell_lot = models.IntegerField(validators=[MaxValueValidator(2147483647)], null=True, blank=True)

    # 交易方向。1表示做多；-1表示做空
    direction = models.IntegerField(validators=[MaxValueValidator(2147483647)], null=True, blank=True)

    # K线运行形态。1表示趋势形态，2表示震荡形态
    k_line_movement_pattern = models.IntegerField(validators=[MaxValueValidator(2147483647)], null=True, blank=True)

    # 方差类型。variance20表示20日方差，variance60表示60日方差，variance120表示120日方差
    variance_type = models.CharField(max_length=32, db_index=True, null=True, blank=True)

    # 方差取值（使得区分趋势和震荡形态最准确）
    variance_value = models.BigIntegerField(validators=[MaxValueValidator(9223372036854775807)], null=True, blank=True)

    # 均线类型。ma60表示60日均线，ma120表示120日均线，ma250表示250日均线
    ma_type = models.CharField(max_length=32, db_index=True, null=True, blank=True)

    # 均线回溯时间
    ma_backtrack = models.BigIntegerField(validators=[MaxValueValidator(9223372036854775807)], null=True, blank=True)

    # 盈亏额（除以保证金，乘以交易乘数之后的）
    profit_and_loss = models.DecimalField(max_digits=32, decimal_places=16, null=True, blank=True)

    # 盈亏率（除以保证金，乘以交易乘数之后的）。单位%
    profit_and_loss_rate = models.DecimalField(max_digits=32, decimal_places=16, null=True, blank=True)

    # 累计收益率
    accumulative_profit_and_loss_rate = models.DecimalField(max_digits=32, decimal_places=16, null=True, blank=True)

    class Meta:
        managed = True
        db_table = 'C_F_ANALYSIS_DETAIL'
        verbose_name = '商品期货分析（详细）'
        verbose_name_plural = verbose_name
        # 单列索引
        # indexes = [
        #     models.Index(fields=['code']),
        #     models.Index(fields=['k_line_period_type']),
        #     models.Index(fields=['begin_time']),
        #     models.Index(fields=['end_time']),
        #     models.Index(fields=['name']),
        # ]
        # 联合索引
        # index_together = [('code', 'buy_date', 'sell_date')]
